<template>
  <div class="employees-container">
    <div class="app-container">
      <!-- 通用工具栏组件使用 -->
      <page-tools>
        <!-- 自定义左侧内容 -->
        <template #slot-left>
          <span>共19条记录</span>
        </template>

        <!-- 自定义右侧内容 -->
        <template #slot-right>
          <el-button type="danger" size="small">导入excel</el-button>
          <el-button type="success" size="small">导出excel</el-button>
          <el-button type="primary" size="small" @click="showDialog = true">新增员工</el-button>
        </template>
      </page-tools>

      <el-card style="margin-top: 10px">
        <el-table border :data="employeesList">
          <el-table-column label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="头像" prop="staffPhoto" />
          <el-table-column label="手机号" prop="mobile" />
          <el-table-column label="工号" prop="workNumber" />
          <el-table-column label="聘用形式">
            <template v-solt="scope">
              <span>{{ formatEmployeeFn(0) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" prop="timeOfEntry" />
          <el-table-column label="操作" width="280">
            <template v-slot="scope">
              <el-button type="text" size="small">查看</el-button>
              <el-button type="text" size="small" @click="setEmp(scope.row.id)">分配角色</el-button>
              <el-button type="text" size="small">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row
          type="flex"
          justify="center"
          align="middle"
          style="height: 60px"
        >
          <!-- 分页区域 -->
          <el-pagination
            :current-page="query.page"
            :page-sizes="[10, 15, 20, 25]"
            :page-size="query.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          /> </el-row></el-card>
      <el-dialog title="新增员工" :visible.sync="showDialog">
        <emp-form @close="closeFn" />
      </el-dialog>
      <!-- 员工-分配角色权限 - 弹窗 -->
      <el-dialog title="分配角色" :visible="showRoleDialog" @close="cancleDialog">
        <!-- 设置角色组件 -->
        <assign-role-dialog
          ref="roleDialog"
          :user-id="employeeId"
          :role-list="roleList"
          @close="showRoleDialog = false"
          @confirm="confirmFn"
        />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getEmployeeListAPI, getRolesAPI, getUserDetailByIdAPI, assignRolesAPI } from '@/api'
import empForm from './components/empForm.vue'
import AssignRoleDialog from './components/assignRoleDialog.vue'
export default {
  name: 'Employees',
  components: {
    empForm,
    AssignRoleDialog
  },
  data() {
    return {
      query: {
        page: 1, // 页码
        size: 10 // 每页条数
      },
      employeesList: [], // 员工列表
      total: 0, // 数据总条数
      showDialog: false, // 添加员工组件的展示
      showRoleDialog: false, // 角色分配弹窗显示
      employeeId: 0, // 角色分配 员工 id
      roleIds: [], // 被选中项
      roleList: [] // 角色列表数据
    }
  },
  created() {
    this.getEmployeeList()
    this.getRoleListFn()
  },
  methods: {
    // 获取员工列表
    async getEmployeeList() {
      // 调用获取员工列表的 API
      const res = await getEmployeeListAPI(this.query)
      console.log(res)
      // 根据返回的状态码给用户提示
      if (!res.success) return this.$message.error(res.message)
      // 将返回的结果赋值 data 中的数据
      this.employeesList = res.data.rows
      this.total = res.data.total
    },
    formatEmployeeFn(str) {
      if (String(str) === '1' || str === '正式') {
        return '正式'
      } else {
        return '非正式'
      }
    },
    // 每页显示的条数发生改变时触发
    handleSizeChange(newSize) {},

    // 当前页面发生改变时触发
    handleCurrentChange(newPage) {},
    closeFn() {
      this.showDialog = false
    },
    // 关闭分配角色弹框
    cancleDialog() {
      this.showRoleDialog = false
      this.$refs.roleDialog.roleIdArr = []
    }, // 分配角色
    async setEmp(id) {
      this.employeeId = id
      this.showRoleDialog = true
      const res = await getUserDetailByIdAPI(id)
      this.$refs.roleDialog.roleIdArr = res.data.roleIds || []
    },
    // 获取角色列表
    async getRoleListFn() {
      const res = await getRolesAPI()
      this.roleList = res.data.rows
    },
    // 角色分配  弹窗 - 确认事件
    async confirmFn(roleIdArr) {
      const res = await assignRolesAPI({
        id: this.employeeId,
        roleIds: roleIdArr
      })
      if (res.success) this.$message.success
      this.cancleDialog()
    }
  }
}
</script>

<style lang="scss" scoped></style>
